import { _decorator, Node, PageView, find, tween,} from 'cc';
import { Vec3 } from 'cc';
import { Component, Label } from 'cc';
import { App } from '../App';
import UtilTool from '../tools/UtilTool';
const { ccclass, property } = _decorator;

// 引导界面
@ccclass('GuidePanel')
export class GuidePanel extends Component {
    @property(Node)
    contentNode: Node = null;

    @property(Node)
    pageView: Node = null;
   
    private selIndex = 0
    private maxIndex = 2
    onLoad() {
        UtilTool.setScreen(this.node)
        UtilTool.nodeActive(this.contentNode, 4, 0.16)
    }

    start() {
        this.setScrollPage(this.selIndex)

        // 手势动画
        /* for (let i = 0; i < 3; i++) {
            let handSpr = find("view/content/page" + (i+1) + "/handSpr",this.pageView)
            
            let embedTween = tween(handSpr)
                .by(0.5, { position: new Vec3(10, -27, 0) })
                .by(0.5, { position: new Vec3(-10, 27, 0) })
                .call(() => {
                })
            tween(handSpr)
                .repeatForever(embedTween)
                .start()


            let tip = find("view/content/page" + (i+1) + "/tip",this.pageView)
            tip.getComponent(Label).fontSize = App.language.languageId_default==App.language.language_ChineseId?42:34
        } */

        let handNode1 = find("view/content/page1/handNode",this.pageView)
        let handSpr2 = find("view/content/page2/handSpr",this.pageView)
        let handSpr3 = find("view/content/page3/handSpr",this.pageView)

        // 手势1动画
        let embedTween1 = tween(handNode1)
            .by(1.0, { angle: -180 })
            .by(1.0, { angle: 180 })
        tween(handNode1)
            .repeatForever(embedTween1)
            .start()

        // 手势2动画
        let embedTween2 = tween(handSpr2)
            .by(0.5, { position: new Vec3(0, -360, 0) })
            .delay(0.5)
            .by(0, { position: new Vec3(0, 360, 0) })
            .call(() => {
            })
        tween(handSpr2)
            .repeatForever(embedTween2)
            .start()

        // 手势3动画
        let embedTween3_1 = tween(handSpr3)
            .by(1.0, { position: new Vec3(0, 40, 0) })
        let embedTween3_2 = tween(handSpr3)
            .to(1.0, {scale: new Vec3(0.3, 0.3, 1)})
        let embedTween3_3 = tween(handSpr3)
            .parallel(embedTween3_1,embedTween3_2)
            .call(() => {
                handSpr3.setPosition(390,70,0)
                handSpr3.setScale(0.4,0.4,1)
            })
        tween(handSpr3)
            .repeatForever(embedTween3_3)
            .start()


    }

    /** 
     * 设置界面
    */
    setScrollPage(index){
        let pageView = this.pageView.getComponent(PageView)
        pageView.scrollToPage(index,0.3)
        pageView.setCurrentPageIndex(index)
    }
    
    /* 翻页回调 */
    onPaveView(event : PageView, eventType) {
        this.selIndex = event.getCurrentPageIndex()

        let wz = find("Btn_next/Label",this.contentNode).getComponent(Label)
        if (this.selIndex == this.maxIndex) { // 第三步
            wz.string = App.language.getLanguageLab(105)
        }
        else{
            wz.string = App.language.getLanguageLab(109)
        }
    }

    /** 
     * 下一步
      */
    clickNext() {
        App.Audio.playOneShot("click")
        this.selIndex = this.selIndex + 1

        if (this.selIndex < this.maxIndex) {    // 第二步
            this.setScrollPage(this.selIndex)
        }
        else if (this.selIndex == this.maxIndex) { // 第三步
            let wz = find("Btn_next/Label",this.contentNode).getComponent(Label)
            wz.string = App.language.getLanguageLab(105)
            this.setScrollPage(this.selIndex)
        }
        else { // 完成
            this.node.destroy()
        }
    }

}

